package com.ruoyi.rsikManage.service.Impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;


import com.ruoyi.rsikManage.domain.*;
import com.ruoyi.rsikManage.domain.vo.AlarmOfEachDayVo;
import com.ruoyi.rsikManage.domain.vo.AlarmOfEachLevelVo;
import com.ruoyi.rsikManage.domain.vo.HistogramVo;
import com.ruoyi.rsikManage.domain.vo.RealTimeAlarmCountVo;
import com.ruoyi.rsikManage.mapper.TAlarmsMapper;
import com.ruoyi.rsikManage.service.AlgorithmService;
import com.ruoyi.rsikManage.service.ITAlarmsService;
import com.ruoyi.rsikManage.service.ITVideoStandPlaceService;
import com.ruoyi.rsikManage.service.MonitorPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 报警信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-08
 */
@Service
public class TAlarmsServiceImpl implements ITAlarmsService
{
    @Autowired
    private TAlarmsMapper tAlarmsMapper;
    @Autowired
    private ITVideoStandPlaceService  itVideoStandPlaceService;
    @Autowired
    private MonitorPointService monitorPointService;
    @Autowired
    private AlgorithmService algorithmService;


    @Override
    public int countUnhandledAiAlarmInProvinceFileForToday(String placeCategoryDicVal) {
        return tAlarmsMapper.countUnhandledAiAlarmInProvinceFileForToday(placeCategoryDicVal);
    }

    @Override
    public int countHandlingAiAlarmInProvinceFileForToday(String placeCategoryDicVal) {
        return tAlarmsMapper.countHandlingAiAlarmInProvinceFileForToday(placeCategoryDicVal);
    }

    @Override
    public int countHandledAiAlarmInProvinceFileForToday(String placeCategoryDicVal) {
        return tAlarmsMapper.countHandledAiAlarmInProvinceFileForToday(placeCategoryDicVal);
    }

    @Override
    public int countAllAiAlarmInProvinceFileForToday(String placeCategoryDicVal) {
        return tAlarmsMapper.countAllAiAlarmInProvinceFileForToday(placeCategoryDicVal);
    }

    @Override
    public int insertGroundAlarmSituationOfCurrent(int unhandledAlarmCount, int handledAlarmCount, int alarmCount) {
        return tAlarmsMapper.insertGroundAlarmSituationOfCurrent(unhandledAlarmCount, handledAlarmCount, alarmCount, unhandledAlarmCount > 0 ? 1 : 0);
    }

    @Override
    public int insertUndergroundKeyPositionsAlarmSituationOfCurrent(int unhandledAlarmCount, int handledAlarmCount, int alarmCount) {
        return tAlarmsMapper.insertUndergroundKeyPositionsAlarmSituationOfCurrent(unhandledAlarmCount, handledAlarmCount, alarmCount, unhandledAlarmCount > 0 ? 1 : 0);
    }

    @Override
    public int insertKeyMinesUndergroundAlarmSituationOfCurrent(int unhandledAlarmCount, int handledAlarmCount, int alarmCount) {
        return tAlarmsMapper.insertKeyMinesUndergroundAlarmSituationOfCurrent(unhandledAlarmCount, handledAlarmCount, alarmCount, unhandledAlarmCount > 0 ? 1 : 0);
    }

    @Override
    public List<AlarmEvent> selectAlarmEventList(AlarmEvent alarmEvent) {
        return tAlarmsMapper.selectAlarmEventList(alarmEvent);
    }

    @Override
    public int markAlarmEventAsSent(List<AlarmEvent> alarmEvents) {
        return tAlarmsMapper.markAlarmEventAsSent(alarmEvents);
    }
    @Override
    public List<PlaceCategory> countAlarmOfCategoryAndPlace(Date startTime, Date endTime) {
        List<PlaceCategory> categoryList = tAlarmsMapper.countAlarmOfCategoryAndPlace(startTime, endTime);
        for (PlaceCategory category : categoryList) {
            int count = 0;
            List<TVideoStandPlace> placeList = category.getPlaceList();
            for (TVideoStandPlace place : placeList) {
                Integer placeAlarmCount = place.getPlaceAlarmCount();
                count += placeAlarmCount;
            }
            category.setCategoryAlarmCount(count);
        }
        return categoryList;
    }

    @Override
    public List<PlaceCategory> countAlarmOfCategoryAndPlaceForUnitCodes(LocalDateTime startTime, LocalDateTime endTime, List<Long> unitCodes) {
        List<PlaceCategory> categoryList = tAlarmsMapper.countAlarmOfCategoryAndPlaceForUnitCodes(startTime, endTime,unitCodes);
        for (PlaceCategory category : categoryList) {
            int count = 0;
            List<TVideoStandPlace> placeList = category.getPlaceList();
            for (TVideoStandPlace place : placeList) {
                Integer placeAlarmCount = place.getPlaceAlarmCount();
                count += placeAlarmCount;
            }
            category.setCategoryAlarmCount(count);
        }
        return categoryList;
    }

    @Override
    public List<PlaceCategory> countAlgoOfCategory() {
        //查询省文件所有场所类别
        List<PlaceCategory> placeCategoryDicList = itVideoStandPlaceService.selectCategoryListWithPlacesWithMonitors();
        for (PlaceCategory category : placeCategoryDicList) {
            int algoCountOfCategory = 0;
            List<TVideoStandPlace> placeList = category.getPlaceList();
            if (placeList == null) placeList = new ArrayList<>();
            for (TVideoStandPlace place : placeList) {
                List<MonitorPoint> monitorList = place.getMonitorList();
                if (monitorList == null) monitorList = new ArrayList<>();
                for (MonitorPoint monitor : monitorList) {
                    List<Algorithm> algoList = algorithmService.algorithmsInTheMonitor(monitor.getApeId());
                    algoCountOfCategory += algoList.size();
                }
            }
            category.setPlaceList(null);
            category.setCategoryAlgorithmCount(algoCountOfCategory);
        }
        return placeCategoryDicList;
    }

    @Override
    public List<AlarmOfEachDayVo> countAlarmOfEachDay(Date startTime, Date endTime, String alarmName) {
        return tAlarmsMapper.countAlarmOfEachDay(startTime, endTime, alarmName);
    }

    @Override
    public List<AlarmOfEachDayVo> countAlarmOfEachDayForUnitCodes(LocalDateTime startTime, LocalDateTime endTime, String alarmName,List<Long> unitCodes) {
        return tAlarmsMapper.countAlarmOfEachDayForUnitCodes(startTime, endTime, alarmName,unitCodes);
    }

    @Override
    public List<AlarmOfEachLevelVo> countAlarmOfEachLevel(Date startTime, Date endTime) {
        return tAlarmsMapper.countAlarmOfEachLevel(startTime, endTime);
    }


    /**
     * 统计某标准文件下的各个场所类别中的:各场所的报警总数、算法总数
     */
    @Override
    public List<PlaceCategory> countPlaceCategoryList(String provinceFileDicVal, Date startTime, Date endTime) {
        //1.查询该标准文件下所有的场所类别
        List<PlaceCategory> placeCategoryDicList = itVideoStandPlaceService.selectPlaceCategoryDicList("t_video_child_level", provinceFileDicVal);
        //2.遍历场所类别列表
        for (PlaceCategory category : placeCategoryDicList) {
            int categoryPlaceCount = 0;
            int categoryMonitorCount = 0;
            int categoryAlarmCount = 0;
            int categoryAlgorithmCount = 0;
            //3.查询该场所类别下的场所
            //构建查询参数
            TVideoStandPlace placeParm = new TVideoStandPlace();
            placeParm.setTVideoParentLevel(provinceFileDicVal);
            placeParm.setTVideoChildLevel(category.getDicValue());
            List<TVideoStandPlace> placeListInThisCategory = itVideoStandPlaceService.selectTVideoStandPlaceList(placeParm);
            categoryPlaceCount += placeListInThisCategory.size();
            //4.遍历场所
            for (TVideoStandPlace place : placeListInThisCategory) {
                //5.查询该场所的监控点
                List<MonitorPoint> monitorListInThisPlace = monitorPointService.selectMonitorPointsInPlace(place.getId());
                int placeAlgorithmCount = 0;
                int placeMonitorCount = monitorListInThisPlace.size();
                int placeAlarmCount = 0;
                //6.遍历监控点
                for (MonitorPoint monitor : monitorListInThisPlace) {
                    //7.查询该监控点的算法列表
                    List<Algorithm> algoListInThisMonitor = algorithmService.algorithmsInTheMonitor(monitor.getApeId());
                    //8.查询某段时间内，该监控点的报警个数
                    Integer countAlarmsInThisMonitor =  tAlarmsMapper.countTAlarmsInThisMonitor(monitor.getApeId(), startTime, endTime);
//                    monitor.setAlgorithmList(algoListInThisMonitor);
                    monitor.setMonitorAlarmCount(countAlarmsInThisMonitor);
                    monitor.setMonitorAlgorithmCount(algoListInThisMonitor.size());
                    placeAlgorithmCount += algoListInThisMonitor.size();
                    placeAlarmCount += countAlarmsInThisMonitor;
                }
                categoryMonitorCount += placeMonitorCount;
                categoryAlarmCount += placeAlarmCount;
                categoryAlgorithmCount += placeAlgorithmCount;
                //place.setMonitorList(monitorListInThisPlace);
                //
                place.setPlaceAlgorithmCount(placeAlgorithmCount);
                //
                place.setPlaceMonitorCount(placeMonitorCount);
                //
                place.setPlaceAlarmCount(placeAlarmCount);
            }
            category.setPlaceList(placeListInThisCategory);
            category.setCategoryPlaceCount(categoryPlaceCount);
            category.setCategoryAlarmCount(categoryAlarmCount);
            category.setCategoryAlgorithmCount(categoryAlgorithmCount);
            category.setCategoryMonitorCount(categoryMonitorCount);
        }
        return placeCategoryDicList;
    }

    @Override
    public List<HistogramVo> countAlgoAlarmForEachType(Date startTime, Date endTime) {
        return tAlarmsMapper.countAlgoAlarmForEachType(startTime, endTime);
    }

    @Override
    public List<HistogramVo>countAlgoAlarmForEachMonth(String alarmName) {
        LinkedList<HistogramVo> resList = new LinkedList<>();
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < 12; i++) {
            int curMonth = calendar.get(Calendar.MONTH) + 1;
            int curYear = calendar.get(Calendar.YEAR);
            String curMonthStr = curMonth + "";
            if (curMonth < 10) {
                curMonthStr = "0" + curMonth;
            }
            HistogramVo histogramVo = new HistogramVo();
            histogramVo.setX_axis(curYear + "-" + curMonthStr);
            histogramVo.setY_axis(tAlarmsMapper.countAlgoAlarmInDate(curYear,curMonth, alarmName));
            resList.addFirst(histogramVo);
            calendar.add(Calendar.MONTH, -1);
        }
        return resList;
    }

    @Override
    public List<HistogramVo>countAlgoAlarmForEachMonthForUnitCodes(LocalDateTime startTime, LocalDateTime endTime,String alarmName,List<Long> unitCodes) {
        LinkedList<HistogramVo> resList = new LinkedList<>();
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < 12; i++) {
            int curMonth = calendar.get(Calendar.MONTH) + 1;
            int curYear = calendar.get(Calendar.YEAR);
            String curMonthStr = curMonth + "";
            if (curMonth < 10) {
                curMonthStr = "0" + curMonth;
            }
            HistogramVo histogramVo = new HistogramVo();
            histogramVo.setX_axis(curYear + "-" + curMonthStr);
            histogramVo.setY_axis(tAlarmsMapper.countAlgoAlarmInDate(curYear,curMonth, alarmName));
            resList.addFirst(histogramVo);
            calendar.add(Calendar.MONTH, -1);
        }
        return resList;
    }


    @Override
    public List<HistogramVo> countOfflineForEachMonitor(Date startTime, Date endTime) {
        return tAlarmsMapper.countOfflineForEachMonitor(startTime, endTime);
    }

    @Override
    public List<HistogramVo> countOfflineForEachMonitorForUnitCodes(LocalDateTime startTime, LocalDateTime endTime,List<Long> unitCodes) {
        return tAlarmsMapper.countOfflineForEachMonitorForUnitCodes(startTime, endTime,unitCodes);
    }



    @Override
    public List<HistogramVo> countOfflineForEachMonth() {
        LinkedList<HistogramVo> resList = new LinkedList<>();
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < 12; i++) {
            int curMonth = calendar.get(Calendar.MONTH) + 1;
            int curYear = calendar.get(Calendar.YEAR);
            String curMonthStr = curMonth + "";
            if (curMonth < 10) {
                curMonthStr = "0" + curMonth;
            }
            HistogramVo histogramVo = new HistogramVo();
            histogramVo.setX_axis(curYear + "-" + curMonthStr);
            histogramVo.setY_axis(tAlarmsMapper.countOfflineInDate(curYear,curMonth));
            resList.addFirst(histogramVo);
            calendar.add(Calendar.MONTH, -1);
        }
        return resList;
    }

    @Override
    public List<HistogramVo> countOfflineForEachMonthForUnitCodes(LocalDateTime startTime, LocalDateTime endTime,List<Long> unitCodes) {
        LinkedList<HistogramVo> resList = new LinkedList<>();
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < 12; i++) {
            int curMonth = calendar.get(Calendar.MONTH) + 1;
            int curYear = calendar.get(Calendar.YEAR);
            String curMonthStr = curMonth + "";
            if (curMonth < 10) {
                curMonthStr = "0" + curMonth;
            }
            HistogramVo histogramVo = new HistogramVo();
            histogramVo.setX_axis(curYear + "-" + curMonthStr);
            histogramVo.setY_axis(tAlarmsMapper.countOfflineInDateForUnitCodes(curYear,curMonth,unitCodes));

            resList.addFirst(histogramVo);
            calendar.add(Calendar.MONTH, -1);
        }
        return resList;
    }

    @Override
    public List<HistogramVo> countDiagnosisAbnorForEachMonitor(Date startTime, Date endTime) {
        return tAlarmsMapper.countDiagnosisAbnorForEachMonitor(startTime, endTime);
    }

    @Override
    public List<HistogramVo> countDiagnosisAbnorForEachMonitorForUnitCodes(LocalDateTime startTime, LocalDateTime endTime,List<Long> unitCodes) {
        return tAlarmsMapper.countDiagnosisAbnorForEachMonitorForUnitCodes(startTime, endTime,unitCodes);
    }



    @Override
    public List<HistogramVo>countDiagnosisAlarmForEachMonth(Date startTime, Date endTime) {
        LinkedList<HistogramVo> resList = new LinkedList<>();
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < 12; i++) {
            int curMonth = calendar.get(Calendar.MONTH) + 1;
            int curYear = calendar.get(Calendar.YEAR);
            String curMonthStr = curMonth + "";
            if (curMonth < 10) {
                curMonthStr = "0" + curMonth;
            }
            HistogramVo histogramVo = new HistogramVo();
            histogramVo.setX_axis(curYear + "-" + curMonthStr);
            histogramVo.setY_axis(tAlarmsMapper.countDiagnosisAlarmInDate(curYear,curMonth));
            resList.addFirst(histogramVo);
            calendar.add(Calendar.MONTH, -1);

        }
        return resList;
    }

    @Override
    public List<HistogramVo>countDiagnosisAlarmForEachMonthForUnitCodes(LocalDateTime startTime, LocalDateTime endTime,List<Long> unitCodes) {
        LinkedList<HistogramVo> resList = new LinkedList<>();
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < 12; i++) {
            int curMonth = calendar.get(Calendar.MONTH) + 1;
            int curYear = calendar.get(Calendar.YEAR);
            String curMonthStr = curMonth + "";
            if (curMonth < 10) {
                curMonthStr = "0" + curMonth;
            }
            HistogramVo histogramVo = new HistogramVo();
            histogramVo.setX_axis(curYear + "-" + curMonthStr);
            histogramVo.setY_axis(tAlarmsMapper.countDiagnosisAlarmInDateForUnitCodes(curYear,curMonth,unitCodes));
            resList.addFirst(histogramVo);
            calendar.add(Calendar.MONTH, -1);

        }
        return resList;
    }

    @Override
    public List<HistogramVo> countAlarmLevel() {
        List<HistogramVo> res = new ArrayList<>(3);
        HistogramVo histogramVo_1 = new HistogramVo();
        histogramVo_1.setX_axis("一级报警");
        histogramVo_1.setY_axis(tAlarmsMapper.countAlarmOfAlarmLevel("1"));
        HistogramVo histogramVo_2 = new HistogramVo();
        histogramVo_2.setX_axis("二级报警");
        histogramVo_2.setY_axis(tAlarmsMapper.countAlarmOfAlarmLevel("2"));
        HistogramVo histogramVo_3 = new HistogramVo();
        histogramVo_3.setX_axis("三级报警");
        histogramVo_3.setY_axis(tAlarmsMapper.countAlarmOfAlarmLevel("3"));
        res.add(histogramVo_1 );
        res.add(histogramVo_2);
        res.add(histogramVo_3);
        return res;
    }

    @Override
    public List<Map<String,Object>> countAlarmType(Date startTime, Date endTime) {
        List<Map<String, Object>> maps = tAlarmsMapper.countAlarmType(startTime, endTime);
        return maps;
    }

    @Override
    public List<Map<String,Object>> countAlarmTypeForUnitCodes(LocalDateTime startTime, LocalDateTime endTime,List<Long> unitCodes) {
        List<Map<String, Object>> maps = tAlarmsMapper.countAlarmTypeForUnitCodes(startTime, endTime,unitCodes);
        return maps;
    }

//    @Override
//    public String selectTheURLWhereTheAlarmInfoIsSentTo() {
//        return tAlarmsMapper.selectTheURLWhereTheAlarmInfoIsSentTo();
//    }
//
//    @Override
//    public int setTheURLWhereTheAlarmInfoIsSentTo(String url) {
//        return tAlarmsMapper.setTheURLWhereTheAlarmInfoIsSentTo(url);
//    }
//
//    @Override
//    public List<RealTimeAlarmCountVo> selectGroundAlarmSituation() {
//        return tAlarmsMapper.selectGroundAlarmSituation();
//    }
//
//    @Override
//    public List<RealTimeAlarmCountVo> selectUndergroundKeyPositionsAlarmSituation() {
//        return tAlarmsMapper.selectUndergroundKeyPositionsAlarmSituation();
//    }
//
//    @Override
//    public List<RealTimeAlarmCountVo> selectKeyMinesUndergroundAlarmSituation() {
//        return tAlarmsMapper.selectKeyMinesUndergroundAlarmSituation();
//    }
//
    /**
     * 查询报警信息
     *
     * @param idId 报警信息ID
     * @return 报警信息
     */
//    @Override
//    public TAlarms selectTAlarmsById(Long idId)
//    {
//        return tAlarmsMapper.selectTAlarmsById(idId);
//    }


//    @Override
//    public List<TAlarms> selectTAlarmsList(TAlarms tAlarms)
//    {
//        return tAlarmsMapper.selectTAlarmsList(tAlarms);
//    }


//    @Override
//    public int insertTAlarms(TAlarms tAlarms)
//    {
//        return tAlarmsMapper.insertTAlarms(tAlarms);
//    }


//    @Override
//    public int updateTAlarms(TAlarms tAlarms)
//    {
//        return tAlarmsMapper.updateTAlarms(tAlarms);
//    }


//    @Override
//    public int deleteTAlarmsByIds(Long[] idIds)
//    {
//        return tAlarmsMapper.deleteTAlarmsByIds(idIds);
//    }

//    @Override
//    public int deleteTAlarmsById(Long idId)
//    {
//        return tAlarmsMapper.deleteTAlarmsById(idId);
//    }


//    @Override
//    public List<HashMap<String, Object>> countAiAlarmForEachDay(Long placeId, String alarmType) {
//        List<HashMap<String, Object>> resList = new ArrayList<>();
//
//        LocalDate date = LocalDate.now();
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//
//        for (int i = 0; i < 12; i++) {
//            int year = date.getYear();
//            int monthValue = date.getMonthValue();
//            int dayOfMonth = date.getDayOfMonth();
//            int alarmCount = tAlarmsMapper.countAiAlarm(placeId, alarmType,year, monthValue, dayOfMonth);
//
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("time",date.format(formatter));
//            map.put("alarmCount", alarmCount);
//            resList.add(map);
//
//            date = date.minusDays(1);
//        }
//        return resList;
//    }

//    @Override
//    public List<HashMap<String, Object>> countAiAlarmForEachMonth(Long placeId, String alarmType) {
//        List<HashMap<String, Object>> resList = new ArrayList<>();
//
//        LocalDate date = LocalDate.now();
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//
//        for (int i = 0; i < 12; i++) {
//            int year = date.getYear();
//            int monthValue = date.getMonthValue();
//            int dayOfMonth = date.getDayOfMonth();
//            int alarmCount = tAlarmsMapper.countAiAlarm(placeId, alarmType,year, monthValue, dayOfMonth);
//
//            HashMap<String, Object> map = new HashMap<>();
//
//            map.put("time",date.format(formatter));
//            map.put("alarmCount", alarmCount);
//            resList.add(map);
//
//            date =  date.minusMonths(1);
//        }
//        return resList;
//    }

//    @Override
//    public List<HashMap<String, Object>> countAiAlarmForEachYear(Long placeId, String alarmType) {
//        List<HashMap<String, Object>> resList = new ArrayList<>();
//
//        LocalDate date = LocalDate.now();
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//
//        for (int i = 0; i < 12; i++) {
//            int year = date.getYear();
//            int monthValue = date.getMonthValue();
//            int dayOfMonth = date.getDayOfMonth();
//            int alarmCount = tAlarmsMapper.countAiAlarm(placeId, alarmType,year, monthValue, dayOfMonth);
//
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("time",date.format(formatter));
//            map.put("alarmCount", alarmCount);
//            resList.add(map);
//
//            date =  date.minusYears(1);
//        }
//        return resList;
//    }

//    @Override
//    public int countAiAlarmInPlaceOverAPeriodOfTime(Long placeId, String alarmType, Date startTime, Date endTime) {
//
//        return 0;
//    }
//    @Override
//    public List<HistogramVo> countOfflineForEachMonitor2(Date startTime, Date endTime) {
//        ArrayList<HistogramVo> resList = new ArrayList<>();
//        List<MonitorPoint> monitorPointList = monitorPointService.selectAllMonitorPoints();
//        for (MonitorPoint m: monitorPointList) {
//            HistogramVo histogramVo = new HistogramVo();
//            histogramVo.setX_axis(m.getName());
//            histogramVo.setY_axis(tAlarmsMapper.countOffline(m.getApeId(), startTime, endTime));
//            resList.add(histogramVo);
//        }
//        Collections.sort(resList);
//        return resList;
//    }
//    @Override
//    public List<HistogramVo> countDiagnosisAbnorForEachMonitor2(Date startTime, Date endTime) {
//        ArrayList<HistogramVo> resList = new ArrayList<>();
//        List<MonitorPoint> monitorPointList = monitorPointService.selectAllMonitorPoints();
//        for (MonitorPoint m: monitorPointList) {
//            HistogramVo histogramVo = new HistogramVo();
//            histogramVo.setX_axis(m.getName());
//            histogramVo.setY_axis(tAlarmsMapper.countDiagnosisAbnor(m.getApeId(), startTime, endTime));
//            resList.add(histogramVo);
//        }
//        Collections.sort(resList);
//        return resList;
//    }
}
